﻿using aspnetcore.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using aspnetcore.Repositories;
using aspnetcore.Interfaces;
using System;
using Autofac;

namespace aspnetcore
{
    public class Startup
    {
        public Startup(IConfiguration config, IHostingEnvironment env)
        {
            Configuration = config;
            HostingEnvironment = env;
        }

        public IConfiguration Configuration { get; }
        public IHostingEnvironment HostingEnvironment { get; }

        // (可选) 此方法定义应用所使用的服务（如 ASP.NET Core MVC、Entity Framework Core、Identity）, 在运行时被调用.
        // 先于 Configure 方法调用
        // public void ConfigureServices(IServiceCollection services)
        // {
        //     // Add framework services.
        //     var connectionString = "Data Source=192.168.20.54;port=3306;Initial Catalog=youjihs;uid=root;pwd=1qazXSW@;persistsecurityinfo=True;SslMode=none;";
        //     services.AddDbContext<HSContext>(options => options.UseMySQL(connectionString));

        //     services.AddMvc();

        //     services.AddScoped<ISysConfigRepository, SysConfigRepository>();

        //     // Add application services.
        //     // services.AddTransient();    // 瞬时 - 每一次GetService都会创建一个新的实例.
        //     // services.AddScoped();       // Scope - 在同一个Scope内只初始化一个实例, 可以理解为(每一个request级别只创建一个实例, 同一个http request会在一个 scope内).
        //     // services.AddSingleton();    // 单例 - 整个应用程序生命周期以内只创建一个实例

        //     services.AddTransient<IOperationTransient, Operation>();
        //     services.AddScoped<IOperationScoped, Operation>();
        //     services.AddSingleton<IOperationSingleton, Operation>();
        //     services.AddSingleton<IOperationSingletonInstance>(new Operation());
            
        //     services.AddTransient<OperationService>();
        // }

        public IServiceProvider ConfigureServices(IServiceCollection services) {

            services.AddMvc();
            
            var containerBuilder = new ContainerBuilder();

            return null;
        }

        // (必须) 此方法定义请求管道的中间件, 在运行时被调用.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
